//
//  SocialVideoBrowserViewController.m
//  ShowYork
//
//  Created by hxh on 2020/9/23.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "DynamicVideoBrowserViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface DynamicVideoBrowserViewController ()
@property (nonatomic,strong)UIView *toolView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)UIButton *removeBtn;
@property (nonatomic,strong)AVPlayer *player;
@property (nonatomic,strong)UIButton *coverBtn;
@property (nonatomic,assign)BOOL isShow;
@end

@implementation DynamicVideoBrowserViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
   
    self.isHideNavBar = YES;
    
    // 设置播放的项目
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:self.videoUrl]];
    if (self.player == nil) {
       self.player = [[AVPlayer alloc] init];
    }
    [self.player replaceCurrentItemWithPlayerItem:playerItem];
    
    
    AVPlayerLayer *avLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    avLayer.backgroundColor = (__bridge CGColorRef _Nullable)([UIColor blackColor]);
    avLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    avLayer.frame = self.view.frame;
    [self.view.layer addSublayer:avLayer];
    
    [self.player play];
    
    
    self.coverBtn = [[UIButton alloc]initWithFrame:self.view.frame];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.coverBtn];
    
    self.toolView = [[UIView alloc]init];
    self.toolView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.toolView];
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-kNavBarHeight);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(kNavBarHeight);
    }];
    
    
    
    [self setToolSubViews];
    
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTime object:self.player.currentItem];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
    
}

- (void)moviePlayDidEnd:(NSNotification *)note{
    
    AVPlayerItem *item = [note object];

    [item seekToTime:kCMTimeZero];

    [self.player play];
}

- (void)setToolSubViews{
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = [UIFont boldSystemFontOfSize:18];
    self.titleLb.text = @"1/1";
    self.titleLb.textColor = RGBA(17, 17, 17, 1);

    self.backBtn = [[UIButton alloc]init];
    [self.backBtn setImage:[UIImage imageNamed:@"common_back_tag"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.backBtn];
    
    self.removeBtn = [[UIButton alloc]init];
    [self.removeBtn setImage:[UIImage imageNamed:@"social_tag_remove"] forState:UIControlStateNormal];
    [self.removeBtn addTarget:self action:@selector(removeMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.removeBtn];
    
    
    [self.toolView addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.toolView);
        make.bottom.equalTo(self.toolView).offset(-8);
    }];
        
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.toolView).offset(16);
        make.bottom.equalTo(self.toolView).offset(-8);
    }];
    [self.removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.toolView.mas_right).offset(-16);
        make.bottom.equalTo(self.toolView).offset(-8);
    }];
}


- (void)backMethod{
    [self.player pause];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)removeMethod{
    
    [self.player pause];
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要删除此视频吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
        
        if (self.removeVideo) {
            self.removeVideo();
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
        [self.player play];
    }];
    [confirmAction setValue:[RPAppManager themColor] forKey:@"titleTextColor"];
    [cancelAction setValue:[RPAppManager themColor] forKey:@"titleTextColor"];
             //往alertViewController上添加alertAction
    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
    
    
  

}

- (void)coverMethod{
    self.isShow = !self.isShow;
    [self showAnimation];
}

- (void)showAnimation{
    CGFloat height = 0;
    if (self.isShow) {
        height = 0;
    }else{
        height = -kNavBarHeight;
    }
    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(height);
    }];
}

- (void)dealloc{
    self.player = nil;
}



@end
